//
//  TotalSearchViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/10/17.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "TotalSearchViewController.h"
#import "CCZSegementController2.h"

#import "LiveContentViewController.h"
#import "MallReccomendViewController.h"
#import "JobRecommendListViewController.h"

@interface TotalSearchViewController ()<UISearchBarDelegate>

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) NSString *keyWord;

@property (nonatomic, strong) UISearchBar *searchBar;

// 历史记录
@property (nonatomic, strong) UIView *line1;

@property (nonatomic, strong) MallReccomendViewController *mallVC;
@property (nonatomic, strong) LiveContentViewController *liveVC;
@property (nonatomic, strong) JobRecommendListViewController *jobVC;

@end

@implementation TotalSearchViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self buildUI];
}

#pragma mark ==========构建UI==========
- (void)buildUI {
	self.view.backgroundColor = kWhiteColor;
	self.navigationItem.title = @"综合搜索";
	
	UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(SW(20), SW(40), SCREEN_WIDTH-SW(10), SW(10))];
	[self.view addSubview:searchBar];
	searchBar.placeholder = @"请输入关键字…";
	searchBar.backgroundImage = IMAGE(@"clearImage");
	searchBar.layer.cornerRadius = searchBar.height/2;
	searchBar.layer.masksToBounds = YES;
	searchBar.keyboardType = UIKeyboardTypeDefault;
	searchBar.delegate = self;
	searchBar.searchBarStyle = UISearchBarStyleDefault;
	UIView *searchTextField = searchTextField = [searchBar valueForKey:@"_searchField"];
	searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
	
	[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:kBlueColor];
	[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"取消"];
	
	_line1 = [[UIView alloc] initWithFrame:CGRectMake(0, searchBar.y+searchBar.height+SW(40), SCREEN_WIDTH, 1)];
	[self.view addSubview:_line1];
	_line1.backgroundColor = UISeparatedColorRGB;
	
	_mallVC = [[MallReccomendViewController alloc] init];
	_mallVC.type = 1;
	
	_liveVC = [[LiveContentViewController alloc] init];
	_liveVC.isSearch = @"yes";
	
	_jobVC = [[JobRecommendListViewController alloc] init];
	
    int state = [LZCTools thinkEnv];
	NSArray *nameArr;
    NSArray *vcArr;
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
    if(state != 0){
        if (role == 5 || role == 6) {
            nameArr = @[@"商城", @"直播", @"人才"];
        }
        else {
            nameArr = @[@"商城", @"直播", @"岗位"];
        }
        vcArr = @[_mallVC, _liveVC, _jobVC];
    }
    else {
        if (role == 5 || role == 6) {
            nameArr = @[@"商城", @"人才"];
        }
        else {
            nameArr = @[@"商城", @"岗位"];
        }
        vcArr = @[_mallVC, _jobVC];
    }
	
	
	
	CCZSegementController2 *segement = [CCZSegementController2 segementControllerWithFrame:CGRectMake(0, _line1.y+1, SCREEN_WIDTH, SCREEN_HEIGHT-64-(_line1.y+1)) titles:nameArr];
	[segement setSegementViewControllers:vcArr];
	
	segement.style = CCZSegementStyleDefault;
	[segement setSegementTintColor:UIMainColorRGB];
	[self.view addSubview:segement.view];
	[self addChildViewController:segement];
	__weak typeof(self) weakSelf = self;
	[segement selectedNavAtIndex:^(NSInteger index) {
		NSLog(@"index %ld",index);
		weakSelf.index = index;
	}];
	
	//由于之前为了不和岗位首页的scrollview冲突，默认是不能滑动的，这里需要设置一下
	_jobVC.jobListTableView.scrollEnabled = YES;
}

#pragma mark ==========UISearchBar 代理响应方法==========
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
	return YES;
}
//- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
//    return YES;
//}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
	NSLog(@"开始输入搜索内容");
	
	// 取消字体颜色？
	searchBar.showsCancelButton = YES;
	[searchBar setShowsCancelButton:YES animated:YES];
	UITextField *searchBarTextField = nil;
	NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? searchBar.subviews : [[searchBar.subviews objectAtIndex:0] subviews];
	for (UIView *subview in views)
	{
		if ([subview isKindOfClass:[UITextField class]])
		{
			searchBarTextField = (UITextField *)subview;
			break;
			
		}
	}
	searchBarTextField.enablesReturnKeyAutomatically = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
	NSLog(@"输入搜索完毕");
	
	[searchBar setShowsCancelButton:NO animated:YES];
	[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
	NSLog(@"取消搜索");
	
	[searchBar setShowsCancelButton:NO animated:YES];
	[searchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
	NSLog(@"搜索");
	
	NSString *word = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	
	/*
	if (word.length == 0) {
		[self presentViewController:[LZCTools alertMessage:@"请输入关键字"] animated:YES completion:nil];
		return;
	}
	 */
	
	self.keyWord = word;
	[self searchB];
	[searchBar resignFirstResponder];
}

- (void)searchB {
	
	if ([_searchBar isFirstResponder]) {
		NSLog(@"没有隐藏键盘");
		[_searchBar resignFirstResponder];
	}
	
	//创建通知
	NSNotification *notification;
	notification =[NSNotification notificationWithName:@"searchLive" object:nil userInfo:@{@"keyWord":self.keyWord}];
	//通过通知中心发送通知
	[[NSNotificationCenter defaultCenter] postNotification:notification];
	
	self.mallVC.keyword = self.keyWord;
	self.jobVC.keyword = self.keyWord;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
